package com.cloud.pole.shop.service.user.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.util.jwt.IJWTInfo;
import com.cloud.pole.common.util.jwt.JwtTokenUtil;
import com.cloud.pole.shop.biz.user.ShopUserDeliAddrBiz;
import com.cloud.pole.shop.model.entity.user.ShopUserDeliAddr;
import com.cloud.pole.shop.service.user.UserDeliAddrService;
import lombok.extern.slf4j.Slf4j;

/*
*2019年1月30日
*cloud-user
*author:frank-chen
*/
@Slf4j
@Service
public class UserDeliAddrServiceImpl implements UserDeliAddrService{
	
	@Autowired
	private ShopUserDeliAddrBiz shopUserDeliAddrBiz;
	
	@Override
	public Integer addDeliAddr(String token, String name, String tel, String proId, String cityId, 
			String districtId, String proName, String cityName, String districtName,  String address, Integer defaultTag) throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		if(defaultTag.intValue() == ShopUserDeliAddr.DEFAULT_TAG_YES) {
			ShopUserDeliAddr defaultAddr = shopUserDeliAddrBiz.selectDefaultUserDeliAddr(Long.parseLong(jwtInfo.getId()));
			if(defaultAddr != null) {
				defaultAddr.setDefaultTag(ShopUserDeliAddr.DEFAULT_TAG_NO);
				shopUserDeliAddrBiz.updateSelectiveById(defaultAddr);
			}
		}
		ShopUserDeliAddr userDeliAddr = new ShopUserDeliAddr();
		userDeliAddr.setDeliName(name);
		userDeliAddr.setTel(tel);
		userDeliAddr.setProvinceId(proId);
		userDeliAddr.setCityId(cityId);
		userDeliAddr.setDistrictId(districtId);
		userDeliAddr.setAddress(address);
		userDeliAddr.setProvinceName(proName);
		userDeliAddr.setCityName(cityName);
		userDeliAddr.setDistrictName(districtName);
		userDeliAddr.setCreateTime(new Date());
		userDeliAddr.setUserId(Long.parseLong(jwtInfo.getId()));
		userDeliAddr.setUserName(jwtInfo.getUniqueName());
		userDeliAddr.setDefaultTag(defaultTag);
		shopUserDeliAddrBiz.insertUseGeneratedKeys(userDeliAddr);
		return userDeliAddr.getAddrId();
	}
	
	@Override
	public boolean modifyDeliAddr(String token, Integer addrId, String name, String tel, String proId, String cityId, 
			String districtId, String proName, String cityName, String districtName,  String address, Integer defaultTag) throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		//验证地址id
		ShopUserDeliAddr checkAddr = shopUserDeliAddrBiz.selectByPrimaryKey(addrId);
		if(checkAddr == null || checkAddr.getUserId().longValue()  != Long.parseLong(jwtInfo.getId())) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "addrId错误");
		}	
		if(defaultTag.intValue() == ShopUserDeliAddr.DEFAULT_TAG_YES) {
			ShopUserDeliAddr defaultAddr = shopUserDeliAddrBiz.selectDefaultUserDeliAddr(Long.parseLong(jwtInfo.getId()));
			if(defaultAddr != null && defaultAddr.getAddrId().intValue() != checkAddr.getAddrId().intValue())  {
				defaultAddr.setDefaultTag(ShopUserDeliAddr.DEFAULT_TAG_NO);
				shopUserDeliAddrBiz.updateSelectiveById(defaultAddr);
			}
		}
		ShopUserDeliAddr userDeliAddr = new ShopUserDeliAddr();
		userDeliAddr.setAddrId(addrId);
		userDeliAddr.setDeliName(name);
		userDeliAddr.setTel(tel);
		userDeliAddr.setProvinceId(proId);
		userDeliAddr.setCityId(cityId);
		userDeliAddr.setDistrictId(districtId);
		userDeliAddr.setProvinceName(proName);
		userDeliAddr.setCityName(cityName);
		userDeliAddr.setDistrictName(districtName);		
		userDeliAddr.setAddress(address);
		userDeliAddr.setDefaultTag(defaultTag);
		userDeliAddr.setUserId(Long.parseLong(jwtInfo.getId()));
		userDeliAddr.setUserName(jwtInfo.getUniqueName());
		shopUserDeliAddrBiz.updateSelectiveById(userDeliAddr);
		return true;
	}

	@Override
	public ShopUserDeliAddr getDeliAddr(String token, Integer addrId) throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		//验证地址id
		ShopUserDeliAddr shopUserDeliAddr = shopUserDeliAddrBiz.selectByPrimaryKey(addrId);
		if(shopUserDeliAddr == null || shopUserDeliAddr.getUserId().longValue()  != Long.parseLong(jwtInfo.getId())) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "addrId错误");
		}		
		return shopUserDeliAddr;
	}

	@Override
	public boolean delDeliAddr(String token, Integer addrId) throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		//验证地址id
		ShopUserDeliAddr shopUserDeliAddr = shopUserDeliAddrBiz.selectByPrimaryKey(addrId);
		if(shopUserDeliAddr == null || shopUserDeliAddr.getUserId().longValue()  != Long.parseLong(jwtInfo.getId())) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "addrId错误");
		}		
		shopUserDeliAddrBiz.deleteByPrimaryKey(shopUserDeliAddr);
		return true;	
	}

	@Override
	public List<ShopUserDeliAddr> getDeliAddrList(String token) throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		return shopUserDeliAddrBiz.selectListByUserId(Long.parseLong(jwtInfo.getId()));
	}

	@Override
	public ShopUserDeliAddr getDefaultDeliAddr(String token) throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		return shopUserDeliAddrBiz.selectDefaultUserDeliAddr(Long.parseLong(jwtInfo.getId()));
	}

	@Override
	public boolean setDefaultDeliAddr(String token, Integer addrId) throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		//验证地址id
		ShopUserDeliAddr shopUserDeliAddr = shopUserDeliAddrBiz.selectByPrimaryKey(addrId);
		if(shopUserDeliAddr == null || shopUserDeliAddr.getUserId().longValue()  != Long.parseLong(jwtInfo.getId())) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "addrId错误");
		}		
		ShopUserDeliAddr defaultAddr = shopUserDeliAddrBiz.selectDefaultUserDeliAddr(Long.parseLong(jwtInfo.getId()));
		if(defaultAddr != null && defaultAddr.getAddrId().intValue() != shopUserDeliAddr.getAddrId().intValue())  {
			defaultAddr.setDefaultTag(ShopUserDeliAddr.DEFAULT_TAG_NO);
			shopUserDeliAddrBiz.updateSelectiveById(defaultAddr);
		}
		if(shopUserDeliAddr.getDefaultTag().intValue() == ShopUserDeliAddr.DEFAULT_TAG_NO) {
			shopUserDeliAddr.setDefaultTag(ShopUserDeliAddr.DEFAULT_TAG_YES);
			shopUserDeliAddrBiz.updateSelectiveById(shopUserDeliAddr);
		}
		return true;	
	}


}
